package com.yfjin.gl.filter;

import android.graphics.SurfaceTexture;
import android.opengl.EGL14;
import android.opengl.GLSurfaceView;

import androidx.camera.core.Preview;
import androidx.lifecycle.LifecycleOwner;

import com.yfjin.gl.beauty.BeautyFilter;
import com.yfjin.gl.soul.SoulFilter;
import com.yfjin.gl.soul.Split2Filter;

import java.io.IOException;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

public class CameraRender implements GLSurfaceView.Renderer, Preview.OnPreviewOutputUpdateListener, SurfaceTexture.OnFrameAvailableListener {


    private static final String TAG = "123123";

    private CameraHelper cameraHelper;
    private CamerView camerView;
    private SurfaceTexture cameraTexture;

    private int[] texture;
    private float[] mtx = new float[16];
    private CameraFilter cameraFilter;
    private RecordFilter recordFilter;
    private SoulFilter soulFilter;

    private Split2Filter mSplit2Filter;

    private BeautyFilter mBeautyFilter;

    private MediaRecord_H264 mediaRecord;


    public CameraRender(CamerView camerView) {
        this.camerView = camerView;

        LifecycleOwner lifecycleOwner = (LifecycleOwner) camerView.getContext();
        cameraHelper = new CameraHelper(lifecycleOwner, this);
    }

    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        texture = new int[1];
        //让surfacetext和gpu共享一个数据源
        //会在GPU开辟一个区域，用来放图层
        cameraTexture.attachToGLContext(texture[0]);
        cameraTexture.setOnFrameAvailableListener(this);

        cameraFilter = new CameraFilter(camerView.getContext());
        recordFilter = new RecordFilter(camerView.getContext());
        soulFilter = new SoulFilter(camerView.getContext());
        mSplit2Filter = new Split2Filter(camerView.getContext());
//        mBeautyFilter=new BeautyFilter(camerView.getContext());

        String path = camerView.getContext().getExternalCacheDir().getPath() + "/input.mp4";

        mediaRecord = new MediaRecord_H264(camerView.getContext(), EGL14.eglGetCurrentContext(), path
                , camerView.getWidth(), camerView.getHeight()
        );

    }

    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) {
        recordFilter.setSize(width, height);
        cameraFilter.setSize(width, height);
        soulFilter.setSize(width, height);
        mSplit2Filter.setSize(width, height);
    }

    @Override
    public void onDrawFrame(GL10 gl) {
        cameraTexture.updateTexImage();
        cameraTexture.getTransformMatrix(mtx);
        cameraFilter.setTransformMatrix(mtx);


        int id = cameraFilter.onDraw(texture[0]);
//        id=soulFilter.onDraw(id);
//        id=mSplit2Filter.onDraw(id);

        if (mBeautyFilter != null) {
            id = mBeautyFilter.onDraw(id);
        }


        id = recordFilter.onDraw(id);


        mediaRecord.fireFrame(id, cameraTexture.getTimestamp());
    }


    //-------------------------------------------------------------------------------

    @Override
    public void onUpdated(Preview.PreviewOutput output) {
        cameraTexture = output.getSurfaceTexture();
    }

    //-------------------------------------------------------------------------------
    @Override
    public void onFrameAvailable(SurfaceTexture surfaceTexture) {
        //一帧一帧回调
        camerView.requestRender();
    }


    public void startRecord(float speed) {
        try {
            mediaRecord.start(speed);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void stopRecord() {
        mediaRecord.stop();
    }


    public void enableBeaty(boolean disable) {


//        if (disable) {
//            mBeautyFilter = new BeautyFilter(camerView.getContext());
//            mBeautyFilter.setSize(camerView.getWidth(), camerView.getHeight());
//        } else {
//            mBeautyFilter.release();
//            mBeautyFilter = null;
//        }

        camerView.queueEvent(new Runnable() {
            @Override
            public void run() {
                if (disable) {
                    mBeautyFilter = new BeautyFilter(camerView.getContext());
                    mBeautyFilter.setSize(camerView.getWidth(), camerView.getHeight());
                } else {
                    mBeautyFilter.release();
                    mBeautyFilter = null;
                }
            }
        });

    }
}
